home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / D_NODE.H < prev    next >
C/C++ Source or Header  |  1992-09-28  |  10KB  |  246 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 07/11/89 -- Initial design
  13. // Updated: MBN 08/10/89 -- Initial implementation
  14. // Updated: MBN 08/11/89 -- Inherit from Generic
  15. // Updated: MBN 09/19/89 -- Added conditional exception handling
  16. // Updated: MBN 12/21/89 -- Added optional argument to set_compare method
  17. // Updated: MBN 02/27/90 -- Added operator= for pointer argument
  18. // Updated: MJF 03/12/90 -- Added group names to RAISE
  19. // Updated: VDN 02/21/92 -- New lite version and fix memory leaks
  20. // Updated: JAM 08/19/92 -- modernized template syntax, remove macro hacks
  21. // Updated: JAM 08/19/92 -- insert*() now take non-const Node& since will own
  22. // Updated: JAM 09/28/92 -- added ItemType and max_children members for
  23. //                          new N_Tree convention
  24. //
  25. // The D_Node<Type,nchild> class implements parameterized  nodes of  a  dynamic
  26. // size for N-ary  trees.   This node class  is parameterized for  the type and
  27. // some initial  "N",  the   number  of  subtrees  each  node may   have.   The
  28. // D_Node<Type,nchild>  class  is dynamic   in  the  sense that the   number of
  29. // subtrees allowed for each  node is not  fixed.  D_Node<Type,nchild> utilizes
  30. // the Vector<Type>  class,  supporting runtime growth charactersitics.    As a
  31. // result, the D_Node<Type,nchild> class  should be  used as the node  type for
  32. // the N_Tree<Node,Type,nchild> class when  the number of subtrees is variable,
  33. // unknown at  compile time, or   needs to  increase on   a per-node  basis  at
  34. // runtime.  This capability is suited for heirarchical trees such as  might be
  35. // used  in an   organization  chart.   In  addition,   specialization  of  the
  36. // N_Tree<Node,Type,nchild>  class  would  allow   for  the   relatively   easy
  37. // implementation of a Diagram class.
  38. //
  39. // There are three public constructors for the  D_Node class to allow  the user
  40. // to  create nodes and  control the building and   structure of an  N-ary tree
  41. // where the  ordering   can  have a  specific  meaning.   The  first takes  no
  42. // arguments and initializes the pointer  and data  slots to NULL.   The second
  43. // takes an argument of type Type and initializes  the data slot to that value.
  44. // The third takes a reference to another D_Node object and duplicates the data
  45. // value, but not its children.
  46. //
  47. // The private  data section  contains just two  slots.  The  first is a Vector
  48. // object of some initial size "N"  that  contains pointers to  D_Node objects,
  49. // one for each  child.  The second  is a data slot  of type Type  to hold  the
  50. // value of the data item.
  51. //
  52. // Methods are provided to set and get the node data value, determine if a node
  53. // is a leaf or the root of some subtree,  and implement member-wise assignment
  54. // from one  D_Node to another via  the overloaded operator=. In  addition, the
  55. // brackets operator is overloaded to  provided  a mechanism to efficiently get
  56. // and set individual  subtree pointers in  a specific node,  thus allowing the
  57. // user to   control  the  orgranization  and  structure   of a tree.  Finally,
  58. // insertion, and removal  methods to allow the  user to control  placement and
  59. // ordering of sub-trees of a node are available.
  60. //
  61.  
  62. #ifndef D_NODEH                    // If no definition for class
  63. #define D_NODEH
  64.  
  65. #ifndef MISCELANEOUSH        // If we have not included this file,
  66. #include <misc.h>        // include miscelaneous useful definitions.
  67. #endif
  68.  
  69. #ifndef VECTORH                    // If no Vector class defined
  70. #include <cool/Vector.h>
  71. #endif
  72.  
  73. template <class Node>
  74. class CoolN_Tree;
  75.  
  76. template <class Type, int nchild>
  77. class CoolD_Node {
  78. public:
  79.   typedef Boolean (*Compare)(const Type&, const Type&);
  80.   typedef CoolD_Node<Type,nchild>* CoolD_Node_p; // Pointer to class
  81.  
  82.   typedef Type ItemType;
  83.   static int max_children() { return nchild; }
  84.  
  85.   CoolD_Node();            // Simple constructor
  86.   CoolD_Node(const Type& value);    // constructor with data value
  87.   CoolD_Node(const CoolD_Node<Type,nchild>&);    // Copy constructor
  88.   ~CoolD_Node();            // Destructor
  89.  
  90.   inline void set (const Type&);        // Set node data to value
  91.   inline Type& get () const;            // Get node data value
  92.   Boolean is_leaf () const;            // TRUE if node has no children
  93.   inline CoolD_Node_p& operator[] (int); // Set/Get pointers
  94.   inline int num_subtrees() const;         // number of subtree slots in node
  95.   
  96.   inline void set_compare (Compare = NULL); // Set compare
  97.   inline Boolean operator== (const Type&) const;     // Overload operator==
  98.   inline Boolean operator!= (const Type&) const;     // Overload operator!=
  99.   inline Boolean operator< (const Type&) const;         // Overload operator<
  100.   inline Boolean operator> (const Type&) const;         // Overload operator>
  101.   inline Boolean operator<= (const Type&) const;     // Overload operator<=
  102.   inline Boolean operator>= (const Type&) const;     // Overload operator>=
  103.  
  104.   CoolD_Node<Type,nchild>& operator= (const CoolD_Node<Type,nchild>&); // Assignment
  105.   Boolean insert_before (CoolD_Node<Type,nchild>&, int);     // Insert before 
  106.   Boolean insert_after (CoolD_Node<Type,nchild>&, int);      // Insert after
  107.  
  108. private:
  109. public: //## when friend below works with BC++, delete this line
  110.   CoolVector<CoolD_Node<Type,nchild>*> sub_trees; // Vector of subtree pointers
  111.   Type data;                    // Slot to hold data value
  112.   static Compare compare_s;    // Compare function for class
  113.  
  114.   CoolD_Node<Type,nchild>* copy_nodes(const CoolD_Node<Type,nchild>*) const; // Deep copy
  115.   void index_error (const char* fcn, int i);    // Raise exception
  116.  
  117. //##  friend class CoolN_Tree<CoolD_Node<Type,nchild> >;    // Friend class to access data
  118.   friend int default_CoolD_Node_compare (const Type&, const Type&);
  119. };
  120.  
  121.  
  122. // num_subtrees -- Returns number of slots available for subtrees in node
  123. // Input:          None
  124. // Output:         int length of the CoolVector allocated  for Subtrees.
  125.  
  126. template <class Type, int nchild>
  127. inline int CoolD_Node<Type,nchild>::num_subtrees () const {
  128.   return sub_trees.length();
  129. }
  130.  
  131.  
  132. // set -- Set value of data slot in node
  133. // Input: Reference to data slot value
  134. // Output: None
  135.  
  136. template <class Type, int nchild> 
  137. inline void CoolD_Node<Type,nchild>::set (const Type& value) {
  138.   this->data = value;                // Set data slot value
  139. }
  140.  
  141.  
  142. // get -- Get value of data slot in node
  143. // Input: None
  144. // Output: Reference to data slot value
  145.  
  146. template <class Type, int nchild> 
  147. inline Type& CoolD_Node<Type,nchild>::get () const {
  148.   return ((CoolD_Node<Type,nchild>*)this)->data; // Return data slot value
  149. }
  150.  
  151.  
  152. // set_compare -- Specify the comparison function to be used in logical tests
  153. //                of node data values
  154. // Input:         Pointer to a compare function
  155. // Output:        None
  156.  
  157. template <class Type, int nchild> 
  158. inline void CoolD_Node<Type,nchild>::set_compare (/*CoolD_Node<Type,nchild>::Compare##*/Boolean (*c)(const Type&, const Type&)) {
  159.   if (c == NULL)
  160.     this->compare_s = &default_CoolD_Node_compare; // Default equality
  161.   else
  162.     this->compare_s = c;            // Else use one provided
  163. }
  164.  
  165.  
  166. // operator== -- Overload the equality operator to use the compare function
  167. // Input:        constant reference to Type value
  168. // Output:       TRUE/FALSE
  169.  
  170. template <class Type, int nchild> 
  171. inline Boolean CoolD_Node<Type,nchild>::operator== (const Type& value) const {
  172.   return ((((*this->compare_s)(this->data,value)) == 0) ? TRUE : FALSE);
  173. }
  174.  
  175.  
  176. // operator!= -- Overload the inequality operator to use the compare function
  177. // Input:        constant reference to Type value
  178. // Output:       TRUE/FALSE
  179.  
  180. template <class Type, int nchild> 
  181. inline Boolean CoolD_Node<Type,nchild>::operator!= (const Type& value) const {
  182.   return ((((*this->compare_s)(this->data,value)) == 0) ? FALSE : TRUE);
  183. }
  184.  
  185.  
  186. // operator< -- Overload the less than operator to use the compare function
  187. // Input:       constant reference to Type value
  188. // Output:      TRUE/FALSE
  189.  
  190. template <class Type, int nchild> 
  191. inline Boolean CoolD_Node<Type,nchild>::operator< (const Type& value) const {
  192.   return ((((*this->compare_s)(this->data,value)) < 0) ? TRUE : FALSE);
  193. }
  194.  
  195.  
  196. // operator> -- Overload the greater than operator to use the compare function
  197. // Input:       constant reference to Type value
  198. // Output:      TRUE/FALSE
  199.  
  200. template <class Type, int nchild> 
  201. inline Boolean CoolD_Node<Type,nchild>::operator> (const Type& value) const {
  202.   return ((((*this->compare_s)(this->data,value)) > 0) ? TRUE : FALSE);
  203. }
  204.  
  205.  
  206. // operator<= -- Overload the less than or equal operator to use the compare
  207. //               function
  208. // Input:        constant reference to Type value
  209. // Output:       TRUE/FALSE
  210.  
  211. template <class Type, int nchild> 
  212. inline Boolean CoolD_Node<Type,nchild>::operator<= (const Type& value) const {
  213.   return ((((*this->compare_s)(this->data,value)) > 0) ? FALSE : TRUE);
  214. }
  215.  
  216.  
  217. // operator>= -- Overload the greater than or equal operator to use the compare
  218. //               function
  219. // Input:        constant reference to Type value
  220. // Output:       TRUE/FALSE
  221.  
  222. template <class Type, int nchild> 
  223. inline Boolean CoolD_Node<Type,nchild>::operator>= (const Type& value) const {
  224.   return ((((*this->compare_s)(this->data,value)) < 0) ? FALSE : TRUE);
  225. }
  226.  
  227.  
  228. // operator[] -- Overload the brackets operator to provide a mechanism to set
  229. //               and/or get a sub-tree pointer of a node whose zero-relative
  230. //               index is specified from left to right
  231. // Input:        Zero-relative index into vector of sub-tree pointers
  232. // Output:       Reference to a pointer value
  233.  
  234. template <class Type, int nchild> 
  235. inline /*CoolD_Node<Type,nchild>::CoolD_Node_p##*/CoolD_Node<Type,nchild>*& CoolD_Node<Type,nchild>::operator[](int index) {
  236. #if ERROR_CHECKING
  237.   if (index >= this->num_subtrees())        // If index out of range
  238.     this->index_error ("operator[]", index);    // Raise exception
  239. #endif
  240.   return (this->sub_trees[index]);
  241. }
  242.  
  243.  
  244.  
  245. #endif                        // End D_NODEH #if
  246.